package ControlLayer;
import ModelLayer.AddressBook;
import ModelLayer.Person;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Handles all changes done to AddressBook container and person objects
 */
public class AddressCtr
{
    //declare instance variables to reference containers.
    private int uniqueID = 0;
    private AddressBook addressBook;
    public AddressCtr(){
        addressBook = AddressBook.getInstance();
    }

    /**
     * Seaches AddressBook container for any users with the given paramator(id of type long)
     * This method returns Class Person if any maches if found or null if no maches is found.
     */
    public Person getPerson(int id){
        ArrayList<Person> ab = addressBook.get(); //get ArrayList of persons.
        Iterator <Person> person = ab.iterator(); //Create an iterator and iterates over it to find a match.
        while(person.hasNext()){
            Person p = person.next();
            if(p.getId() == id) return p;
        }
        return null;
    }

    /**µ
     * Creates a new person and adds it to AddressBook container.
     */
    public void createPerson(String name, String address, String postalCode, String city, String phone, String email) {
        addressBook.add(new Person((++uniqueID), name, address, postalCode, city, phone, email));
    }

    /**
     * Deletes a person with the specified id.
     */
    public void deletePersonById(int id) {
        ArrayList<Person> ab = addressBook.get(); //get ArrayList of persons.
        Iterator<Person> person = ab.iterator(); //Create an iterator and iterates over it to delete a person.
        while(person.hasNext()){
            Person p = person.next();
            if(p.getId() == id){
                person.remove();
                addressBook.set(ab);
                System.out.println("Person deleted");
                return;
            }
        }
        System.out.println("Not found");
    }


    public void updatePersonById(int id, String name, String address, String postalCode, String city, String phone, String email) {
        boolean searching = true;
        ArrayList<Person> ab = addressBook.get(); //get ArrayList of persons.
        for(int indx = 0 ; indx < ab.size() && searching ; indx++){ //Sweap for a match and updates it.
            if(ab.get(indx).getId() == id){
                Person person = ab.get(indx);
                person.setName(name);
                person.setAddress(address);
                person.setPostalCode(postalCode);
                person.setCity(city);
                person.setPhone(phone);
                person.setEmail(email);
                ab.set(indx, person);
                searching = false;
            }
        }
        addressBook.set(ab);
    }

    /**
     * Returns all persons in an ArrayList.
     */
    public ArrayList listAllPersons()
    {
        return addressBook.get();
    }
}
